/**
 * Copyright (c) Software Institude of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.jet.codegen;

import java.io.ByteArrayInputStream;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.codegen.jet.JETEmitter;

import edu.pku.sei.modeler.codegen.Activator;
import edu.pku.sei.modeler.jet.util.ModelerConstants;
import edu.pku.sei.modeler.model.core.MetaClass;

/**
 * @author zl
 *
 */
public class GenFigures extends AbstractGenerator {
	private List blocksToGen;
	private List linksToGen;
	private IFolder folder;
	
	public void setBlocksToGen(List blocksToGen) {
		this.blocksToGen = blocksToGen;
	}
	
	public void setLinksToGen(List linksToGen) {
		this.linksToGen = linksToGen;
	}
	
	/* (non-Javadoc)
	 * @see edu.pku.sei.modeler.jet.codegen.AbstractGenerator#generateCode(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public void generateCode(IProgressMonitor monitor)
			throws InterruptedException {
		assert blocksToGen != null;
		assert linksToGen != null;
		assert proj != null;
		String codeBase = ModelerConstants.pkgBase + "." + ModelerConstants.figureBase;
		
		folder = proj.getFolder("src/"+codeBase.replace('.', '/'));
		try {
			folder.create(true, true, monitor);
		} catch (CoreException e) {
			e.printStackTrace();
			throw new InterruptedException("create figure folder failed");
		}
		
		genBlockFigures(monitor);
	}

	private translated.FigureGen genFigure = translated.FigureGen.create("\n");
	private void genBlockFigures(IProgressMonitor monitor) throws InterruptedException {
		try {
/*			String pluginId = Activator.getPluginId();
			String relativeUri = "templates/figures/Figure.javajet";
			
			JETEmitter emitter = new JETEmitter(templatesBase + relativeUri, Activator.getDefault().getClass().getClassLoader());
			emitter.addVariable("JET_TUTORIAL", pluginId);
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.runtime");
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.model.notation");
			emitter.addVariable("JET_TUTORIAL", "org.eclipse.draw2d");
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.gefcommon");
			emitter.addVariable("EMF_ECORE", "org.eclipse.emf.ecore");
			emitter.addVariable("EMF_COMMON", "org.eclipse.emf.common");
*/			
			for(int i=0; i<blocksToGen.size(); i++) {
				if(!(blocksToGen.get(i) instanceof MetaClass))continue;
				MetaClass cls = (MetaClass)blocksToGen.get(i);
//				String result = emitter.generate(monitor, new Object[]{blocksToGen.get(i)});
				String result = genFigure.generate(cls);
				
				IFile javaFile = folder.getFile(cls.getName()+"Figure.java");
				
				ByteArrayInputStream is = new ByteArrayInputStream(result.getBytes());
				javaFile.create(is, true, monitor);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new InterruptedException("generate figure code failed");
		}
	}
}
